package com.czl.module_user.viewmodel

import android.os.Bundle
import androidx.databinding.ObservableField
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.AddRecordBean
import com.czl.base.data.bean.PeopleManagerBean
import com.czl.base.data.bean.RepairListBean
import com.czl.base.data.bean.RoomBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import java.util.ArrayList

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2022/1/6 15:26
 */
class PeopleManagerViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    var roomList = listOf<RoomBean>()
    var houseId = ""
    val houseName = ObservableField("")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<List<PeopleManagerBean>> = SingleLiveEvent()
        val deleteSuccessEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val choiceHouseEvent: SingleLiveEvent<List<RoomBean>> = SingleLiveEvent()
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val deleteEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val renewEvent: SingleLiveEvent<PeopleManagerBean> = SingleLiveEvent()
    }

    val onNavRecordsClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_ADD_RECORDS)
    })

    val onChoiceHouseClick: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.choiceHouseEvent.postValue(roomList)
    })
    val onAddPeopleClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_ADD_PEOPLE, Bundle().apply {
            putInt(AppConstants.BundleKey.PEOPLE_FROM, AddPeopleViewModel.FROM_ADD)
        })
    })
    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getMonthCardList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getMonthCardList()
    })

    private fun getMonthCardList() {
        model.getOwnerRecords(
            model.getAreaId(),
            houseId
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<PeopleManagerBean>>>() {
                override fun onResult(t: BaseBean<List<PeopleManagerBean>>) {
                    if (t.code == 200) {
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }

    fun deleteHouseUser(id: String) {
        model.deleteHouseUser(id)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.code == 200) {
                        uc.deleteSuccessEvent.call()
                        showSuccessToast(t.msg)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }

            })
    }

    fun getUserRooms() {
        model.getUserRooms(model.getLoginPhone().toString(), model.getAreaId())
            .compose(RxThreadHelper.rxSchedulerHelper(this@PeopleManagerViewModel))
            .subscribe(
                object : ApiSubscriberHelper<BaseBean<List<RoomBean>>>() {
                    override fun onResult(t: BaseBean<List<RoomBean>>) {
                        if (t.code == 200) {
                            t.data?.let {
                                roomList = Stream.of(it).filter { e -> e.isHead }
                                    .collect(Collectors.toList()) as List<RoomBean>
                                if (roomList.isNotEmpty()) {
                                    houseName.set(it[0].houseCode)
                                    houseId = it[0].houseId
                                }
                                uc.refreshListEvent.call()
                            }
                        } else {
                            showErrorToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showErrorToast(msg)
                    }

                }
            )
    }
}